package cn.senjoeson.healthsport.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.senjoeson.healthsport.R;
import cn.senjoeson.healthsport.signcalendar.Checked;
import cn.senjoeson.healthsport.signcalendar.DBManager;
import cn.senjoeson.healthsport.signcalendar.SignCalendar;

/**
 * Created by senjoeson on 2018/5/26.
 */

public class CheckOutFragment extends Fragment {


    @BindView(R.id.ll_ee_topbar)
    LinearLayout mLlEeTopbar;
    @BindView(R.id.popupwindow_calendar_month)
    TextView mPopupwindowCalendarMonth;
    @BindView(R.id.popupwindow_calendar)
    SignCalendar mPopupwindowCalendar;
    @BindView(R.id.btn_signIn)
    Button mBtnSignIn;
    @BindView(R.id.ll_popup)
    LinearLayout mLlPopup;
    Unbinder unbinder;


    private String date = null;// 设置默认选中的日期  格式为 “2014-04-05” 标准DATE格式
    private List<String> list = new ArrayList<String>(); //设置标记列表
    DBManager dbManager;
    boolean isinput = false;
    private String date1 = null;//单天日期

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_check_out, container, false);
        unbinder = ButterKnife.bind(this, view);
        initCalendar();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        dbManager.closeDB();
    }



    private void initCalendar() {

        // 初始化DBManager
        dbManager = new DBManager(getActivity());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        date1 = formatter.format(curDate);


        mPopupwindowCalendarMonth.setText(String.format("%d年%d月", mPopupwindowCalendar.getCalendarYear(), mPopupwindowCalendar.getCalendarMonth()));
        if (null != date) {
            int years = Integer.parseInt(date.substring(0,
                    date.indexOf("-")));
            int month = Integer.parseInt(date.substring(
                    date.indexOf("-") + 1, date.lastIndexOf("-")));
            mPopupwindowCalendarMonth.setText(years + "年" + month + "月");

            mPopupwindowCalendar.showCalendar(years, month);
            mPopupwindowCalendar.setCalendarDayBgColor(date,
                    R.mipmap.calendar_date_focused);
        }

        add("2015-11-10");
        add("2015-11-02");
        add("2015-12-02");
        query();
        if (isinput) {
            mBtnSignIn.setText("今日已签，明日继续");
            mBtnSignIn.setBackgroundResource(R.drawable.button_gray);
            mBtnSignIn.setEnabled(false);
        }
        mBtnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Date today = mPopupwindowCalendar.getThisday();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
           /* calendar.removeAllMarks();
           list.add(df.format(today));
           calendar.addMarks(list, 0);*/
                //将当前日期标示出来
                add(df.format(today));
                //calendar.addMark(today, 0);
                query();
                HashMap<String, Integer> bg = new HashMap<String, Integer>();

                mPopupwindowCalendar.setCalendarDayBgColor(today, R.drawable.bg_sign_today);
                mBtnSignIn.setText("今日已签，明日继续");
                mBtnSignIn.setBackgroundResource(R.drawable.button_gray);
                mBtnSignIn.setEnabled(false);
            }
        });
        //监听所选中的日期
//		calendar.setOnCalendarClickListener(new OnCalendarClickListener() {
//
//			public void onCalendarClick(int row, int col, String dateFormat) {
//				int month = Integer.parseInt(dateFormat.substring(
//						dateFormat.indexOf("-") + 1,
//						dateFormat.lastIndexOf("-")));
//
//				if (calendar.getCalendarMonth() - month == 1//跨年跳转
//						|| calendar.getCalendarMonth() - month == -11) {
//					calendar.lastMonth();
//
//				} else if (month - calendar.getCalendarMonth() == 1 //跨年跳转
//						|| month - calendar.getCalendarMonth() == -11) {
//					calendar.nextMonth();
//
//				} else {
//					list.add(dateFormat);
//					calendar.addMarks(list, 0);
//					calendar.removeAllBgColor();
//					calendar.setCalendarDayBgColor(dateFormat,
//							R.drawable.calendar_date_focused);
//					date = dateFormat;//最后返回给全局 date
//				}
//			}
//		});

        //监听当前月份
        mPopupwindowCalendar.setOnCalendarDateChangedListener(new SignCalendar.OnCalendarDateChangedListener() {
            public void onCalendarDateChanged(int year, int month) {
                mPopupwindowCalendarMonth
                        .setText(year + "年" + month + "月");
            }
        });
    }

    public void add(String date) {
        ArrayList<Checked> persons = new ArrayList<Checked>();

        Checked person1 = new Checked(date, "true");

        persons.add(person1);

        dbManager.add(persons);
    }

    public void query() {
        List<Checked> persons = dbManager.query();
        for (Checked person : persons) {
            list.add(person.date);
            if (date1.equals(person.getDate())) {
                isinput = true;
            }
        }
        mPopupwindowCalendar.addMarks(list, 0);
    }
}
